import { formatTDate, isAllEmptyValue } from '@/utils/common'
import { RangePickerValueType } from '@kedacom/react-next'
import { useContext, useState } from 'react'
import { defaultFilterData, FilterContext } from '../context'
const useFilterHook = () => {
	let { params, setParams, filterData, setFilterData, showPasswordDialog, showPieChart } = useContext(FilterContext)
	const INPUT_WIDTH = 140
	const SELECT_WIDTH = 140
	const [timeRangValue, setTimeRangValue] = useState<RangePickerValueType>([null, null])
	const [resetButtonShow, setResetButtonShow] = useState(false)
	const logRiskLevelList = [
		{ value: null, label: '全部风险级别' },
		{ value: 0, label: '紧急' },
		{ value: 1, label: '警报' },
		{ value: 2, label: '关键' },
		{ value: 3, label: '错误' },
		{ value: 4, label: '警告' },
		{ value: 5, label: '通知' },
		{ value: 6, label: '信息' },
		{ value: 7, label: '调试' },
	]
	const eventTypeList = [
		{ value: null, label: '全部操作日志' },
		{ value: 'm', label: '管理员操作日志' },
		// { value: 'a', label: '用户操作日志' },
		// { value: 's', label: '其他日志' },
	]

	/**
	 * 过滤条件改变时，更新数据
	 * @param v 更新后的值
	 * @param keyName 更新的字段
	 */
	const onChange = (v: string, keyName: string) => {
		setFilterData({
			...filterData,
			[keyName]: v,
		})
	}
	// TODO:日期框改变时，那不到起止时间
	const onTimeRangChange = (date: any, dateString: string | any[], value: any) => {
		setTimeRangValue(value)
		if (dateString && dateString.length) {
			let startDate = ''
			let endDate = ''
			if (dateString[0]) {
				startDate = formatTDate(dateString[0] + ' 00:00:00')
			}
			if (dateString[1]) {
				endDate = formatTDate(dateString[1] + ' 23:59:59')
			}
			setFilterData({
				...filterData,
				startDate: startDate,
				endDate: endDate,
			})
		}
	}
	//重置所有数据
	const resetData = () => {
		setFilterData({ ...defaultFilterData })
		setParams({ ...defaultFilterData })
		setResetButtonShow(false)
		setTimeRangValue([null, null])
	}
	/**导出 */
	const onExportLog = () => {
		showPasswordDialog(true)
		/** 导出数据 浏览器下载 */
		/** 通过toast提示 */
		// object_name: params?.nameCodeLevel, // 日志客体名称 subject_name: params?.accountName, // 主体名称level: params?.logRiskLevel, //事件风险级别
		// window.location.href = `/sas/logs/export/${type}?detail=${params.detail}&end_time=${encodeURIComponent(
		// 	params.endDate
		// )}&level=${params.logRiskLevel}&start_time=${encodeURIComponent(params.startDate)}&subject_name=${
		// 	params.accountName
		// }&type=${params.type}&object_name=${params.nameCodeLevel}`
	}
	const onShowPiechart = () => {
		showPieChart(true)
	}
	const onFilterSearch = () => {
		//重新搜索将分页设置成1
		setParams({
			...params,
			...filterData,
			page: 1,
		})
		setResetButtonShow(!isAllEmptyValue(filterData))
	}
	return {
		/**过滤条件集合 */
		filterData,
		/**过滤条件改变时，更新数据 */
		onChange,
		logRiskLevelList,
		eventTypeList,
		onTimeRangChange,
		timeRangValue,
		INPUT_WIDTH,
		SELECT_WIDTH,
		resetData,
		onExportLog,
		onFilterSearch,
		resetButtonShow,
		onShowPiechart,
	}
}
export default useFilterHook
